import { defineStore } from 'pinia'
import { setToken, clearToken } from '@/utils/common/auth'

import router from '@/router'
import { isEmpty } from 'lodash-es'

const registryStoreId = 'registryStoreId'

const useSellerRegisterStore = defineStore('useSellerRegisterStore', {
  state: () => ({
    storeId:
      JSON.parse(sessionStorage.getItem(registryStoreId) as any) || 0 || null,

    registerStore: '/cvmagic/register/store',

    registerQualification: '/cvmagic/register/qualification',

    registerShop: '/cvmagic/register/shop',

    isRouterNext: {},
  }),

  actions: {
    initIsRouterNext() {
      const res = JSON.parse(sessionStorage.getItem('isRouterNext') as any)

      if (res) {
        this.isRouterNext = res
      } else {
        this.isRouterNext[this.registerStore] = true
        this.isRouterNext[this.registerQualification] = false
        this.isRouterNext[this.registerShop] = false
      }
    },

    // 将storeId存入本地
    setStoreId(id: number | string | null) {
      if (isEmpty(id)) {
        sessionStorage.setItem(registryStoreId, JSON.stringify(id))
        this.storeId = id
        return
      }
      sessionStorage.setItem(registryStoreId, JSON.stringify(id))
      this.storeId = id as any
    },

    nextPage(path: string, state: boolean) {
      this.isRouterNext[path] = state
      sessionStorage.setItem('isRouterNext', JSON.stringify(this.isRouterNext))
    },
  },
})
export default useSellerRegisterStore
